package com.nidu.demo.company.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.company.convertor.CompanyConvertor;
import com.nidu.demo.company.dto.CompanyCO;

import com.nidu.demo.company.gateway.CompanyGateway;
import com.nidu.demo.company.model.Company;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 公司单查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class CompanySingleQryExe {

    private final CompanyGateway gateway;

    private final CompanyConvertor convertor;

    public SingleResponse<CompanyCO> execute(Long id) {
        // 1. 查询数据
        Company company = gateway.getById(id);
        if(company == null){
            throw new BizException(ErrorCodeConstants.COMPANY_NOT_FOUND.getCode(), ErrorCodeConstants.COMPANY_NOT_FOUND.getMessage());
        }
        // 2. 转换为DTO返回
        CompanyCO companyCO = convertor.toClientObject(company);

        return SingleResponse.of(companyCO);
    }

}